package com.shiliao.framework;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;

import com.shiliao.framework.db.sqlite.DbUtils;
import com.shiliao.framework.util.StringUtils;
import com.shiliao.framework.util.SystemUtils;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 基础application类
 * Created by yx on 2017/6/2.
 */

public class YimeiFrameworkAppliction extends Application {
    private String dbName = "yimei_db";
    private HashMap<String, DbUtils> dbMap = new HashMap<String, DbUtils>();

    private ArrayList<Activity> allActivitys = new ArrayList<>();
    private static YimeiFrameworkAppliction appliction;

    public static YimeiFrameworkAppliction getInstance() {

        return appliction;
    }

    public void addActivitys(Activity activity) {
        if (allActivitys == null) {
            allActivitys = new ArrayList<>();
        }
        if (activity != null) {
            WeakReference<Activity> weakReference =
                    new WeakReference<Activity>(activity);
            allActivitys.add(weakReference.get());
        }
    }

    public ArrayList<Activity> getAllActivitys() {
        return allActivitys;
    }

    public void removeActivity(Activity activity) {
        if (activity != null
                && allActivitys != null
                && allActivitys.size() > 0) {
            allActivitys.remove(activity);
        }
    }

    public YimeiFrameworkAppliction() {
        if (appliction == null) {
            appliction = this;
        }
    }

    //用于处理在super oncreate之前的操作
    public void onFirstOnCreate() {

    }

    //初始化操作
    public void initApplication() {

    }

    @Override
    public void onCreate() {
        onFirstOnCreate();
        super.onCreate();
       // CrashHandler.create(this);
        setDefaultsConfig();
        initApplication();

    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        OutOfMemoryHandler.gcIfAllocateOutOfHeapSize();
    }

    private void setDefaultsConfig() {
        Resources resources = getResources();
        Configuration configuration = new Configuration();
        configuration.setToDefaults();
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        if (StringUtils.isEmpty(SystemUtils.getUserDeviceID())) {
            SystemUtils.saveDeviceID();
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        exit();
    }

    /**
     * 安全退出应用程序.
     * 退出应用时需要处理运行中保留的状态
     * 如后台下载，通知栏的类容
     */
    public void exit() {
        try {
            // 停止所有状态栏通知
            NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mgr.cancelAll();
            if (allActivitys != null && allActivitys.size() > 0) {
                for (int i = 0; i < allActivitys.size(); i++) {
                    allActivitys.get(i).finish();
                    allActivitys.remove(i);
                }
                allActivitys = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public DbUtils getDb() {
        return getDb(null, this.dbName);
    }

    public DbUtils getDb(String dbDirs, String dbName) {
        String key = dbDirs == null ? dbName : dbDirs + dbName;
        if (dbMap.containsKey(key)) {
            return dbMap.get(key);
        }

        DbUtils db;
        if (dbDirs == null) {
            db = DbUtils.create(this, dbName);
            dbMap.put(dbName, db);
        } else {
            File file = new File(dbDirs);
            if (!file.exists()) {
                file.mkdirs();
            }
            db = DbUtils.create(this, dbDirs, dbName);
            dbMap.put(dbDirs + dbName, db);
        }
        db.configDebug(true);
        db.configAllowTransaction(true);
        return db;
    }
}
